import { Button, Form, Input, message } from 'antd';
import React, { Component } from 'react';
import { instance } from '../util/myrequest';
import axios from 'axios';

class Login extends Component {
    // history
    // constructor(props){
    //     super(props)
    //     this.history = createBrowserHistory({
    //         basename: '',             //基链接
    //         forceRefresh: false        //是否强制刷新
    //     });
    // }
    login = (loginValues) => {
        //1.登录请求
        axios.post('/shopping-user-server/user/login', loginValues)
            .then(response => {
                // 处理返回的数据
                const resp = response.data
                if (resp.success) {
                    localStorage.setItem("token", resp.data.token)
                    localStorage.setItem("user", JSON.stringify(resp.data.user))
                    //todo 清空历史记录
                    // history.replace('/')
                    location.href = '/'
                    return
                } else {
                    message.info(resp.data)
                }
            }).catch(error => {
                // 处理错误
                message.error(error)
            });
    }
    render() {
        const t = localStorage.getItem("token")
        if (t) { location.href = '/' ;return}
        return (
            <div>
                Login<span>如果手机号未进行注册会自动注册</span>
                <Form
                    onFinish={this.login}>
                    <Form.Item
                        rules={[
                            {
                                required: true,
                                pattern: /^1(3[0-9]|4[01456879]|5[0-3,5-9]|6[2567]|7[0-8]|8[0-9]|9[0-3,5-9])\d{8}$/,
                                message: '请输入正确的手机号'
                            }
                        ]}
                        name={'phone'}
                        label={'手机号'}>
                        <Input></Input>
                    </Form.Item>
                    <Form.Item
                        rules={[
                            {
                                required: true,
                                message: '请输入密码',
                            },
                        ]}
                        name={'password'}
                        label={'密码'}>
                        <Input></Input>
                    </Form.Item>
                    <Form.Item>
                        <Button type="primary" htmlType="submit">登录</Button>
                    </Form.Item>
                </Form>
            </div>
        );
    }
}

export default Login;